var util = require('../../libs/util.js');
var app = getApp();
Page({
  data: {
    name: "",
    region: [],
    CO_NAME: '',
    CO_ID: '',
    enterpriseList: [],
    _enterpriseList: [],
    isShare: false,
    isCheckCompany:true
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },
  onLoad: function (e) {
    var that = this;
    wx.setNavigationBarTitle({ "title": app.appName + "--企业注册" });
    wx.showToast({
      title: "页面加载中\n请稍候...",
      icon: "loading",
      duration: 5000
    });
    // this.setData({
    //   driverId: e.id
    // });
    console.log(app.globalData)
  },
  bindRegionChange(e) {
    this.setData({
      region: e.detail.value
    })
  },
  onReady: function () {
    wx.hideToast()
  },
  bindInput: function (e) {
    var item = String(e.currentTarget.id), item_value = {};
    item_value[item] = e.detail.value;
    this.setData(item_value);
  },
  searchCompany:function(e){
    var that = this;
    wx.showLoading({
      title: '验证企业中...',
    });
    util.getData("/wechat/passport/searchCompany", {
      name: e.detail.value
    }, function (res, error) {
      console.log(res); console.log(error)
      wx.hideLoading();
      if (res.data.success) {
        wx.showToast({
          title: res.data.data,
          image: '/images/wrong.png',
          duration: 3000,
          mask: true
        });
        that.setData({
          isCheckCompany:false
        })
      } else {
        that.setData({
          isCheckCompany: true
        })
      }
    });
  },
  submitRegister: function () {
    var that = this;
    wx.showToast({
      title: '不能注册企业',
      image: '/images/wrong.png',
      duration: 3000
    });
    return false;

    if (that.data.isCheckCompany == false){
      wx.showModal({
        content: '企业已存在',
        confirmText: "确定",
        showCancel: false
      });
      return false;
    }
    if (that.data.CO_NAME == '') {
      wx.showModal({
        content: '请输入企业名称',
        confirmText: "确定",
        showCancel: false
      });
      return false;
    }
    if (that.data.region.length == 0) {
      wx.showModal({
        content: '请选择所在区域',
        confirmText: "确定",
        showCancel: false
      });
      return false;
    }
    wx.showLoading({
      title: '请稍候...',
    });
    util.postData("/wechat/passport/enterpriseRegister", {
      region: that.data.region.join(','),
      CO_NAME: that.data.CO_NAME,
      isShare: that.data.isShare ? 1 : 0,
      user_id: app.globalData.user_id,
      phone: app.globalData.mobile,
      openid: app.globalData.openid,
      unionid: app.globalData.unionid
    }, function (res, error) {
      console.log(res); console.log(error)
      wx.hideLoading();
      if (error) {
        wx.showToast({
          title: res.data.data,
          image: '/images/wrong.png',
          duration: 3000
        });
        return false;
      }
      if (res.data.success) {
        wx.showModal({
          title: '温馨提示',
          content: '恭喜你，注册成功！',
          success: function (res) {
            wx.redirectTo({
              url: '/pages/index/index'
            })
          }
        })
      } else if (res.data.success == false) {
        wx.showToast({
          title: res.data.data,
          image: '/images/wrong.png',
          duration: 3000,
          mask: true
        });
      }
    });
  }
});